﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyPSDALayer;

namespace MyPS.Areas.Manage.Controllers
{
    [Authorize]
    public class ManageTechController : Controller
    {
        private MyPSEntities db = new MyPSEntities();

        //
        // GET: /Manage/ManageTech/

        public ActionResult Index()
        {
            return View(db.Teches.OrderByDescending(o=>o.CreateDate).ToList());
        }

        //
        // GET: /Manage/ManageTech/Details/5

        public ActionResult Details(int id = 0)
        {
            Tech tech = db.Teches.Find(id);
            if (tech == null)
            {
                return HttpNotFound();
            }
            return View(tech);
        }

        //
        // GET: /Manage/ManageTech/Create

        public ActionResult Create()
        {
            ViewBag.TypeList = new SelectList(db.TechTypes.ToList(), "TypeName", "TypeName");
            return View();
        }

        //
        // POST: /Manage/ManageTech/Create

        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Tech tech)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var file = Request.Files["fileImgUrl"];
                    if (file != null)
                    {
                        if (file.ContentLength > 204800)
                        {
                            throw new Exception("封面图片超过200kb");
                        }
                        string ex = System.IO.Path.GetExtension(file.FileName);
                        string fileName = Guid.NewGuid().ToString() + ex;
                        string path = Server.MapPath("~/ImgTech/");
                        tech.CoverUrl = string.Format("ImgTech/{0}", fileName);
                        file.SaveAs(path + fileName);
                        tech.CreateBy = HttpContext.User.Identity.Name;
                        tech.CreateDate = DateTime.Now;
                        tech.ModifyBy = HttpContext.User.Identity.Name;
                        tech.ModifyDate = DateTime.Now;
                        tech.ReadCount = 0;
                        db.Teches.Add(tech);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("", "未找到封面图片");
                    }
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
            }

            ViewBag.TypeList = new SelectList(db.TechTypes.ToList(), "TypeName", "TypeName");
            return View(tech);
        }

        //
        // GET: /Manage/ManageTech/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Tech tech = db.Teches.Find(id);
            if (tech == null)
            {
                return HttpNotFound();
            }
            ViewBag.TypeList = new SelectList(db.TechTypes.ToList(), "TypeName", "TypeName");
            return View(tech);
        }

        //
        // POST: /Manage/ManageTech/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Tech tech)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tech).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TypeList = new SelectList(db.TechTypes.ToList(), "TypeName", "TypeName");
            return View(tech);
        }

        //
        // GET: /Manage/ManageTech/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Tech tech = db.Teches.Find(id);
            if (tech == null)
            {
                return HttpNotFound();
            }
            return View(tech);
        }

        //
        // POST: /Manage/ManageTech/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Tech tech = db.Teches.Find(id);
            db.Teches.Remove(tech);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}